package com.yyk.springservicetwo.config;

import com.yyk.config.commonService.enumeration.HttpStatusEnum;
import com.yyk.config.commonService.exception.YYKCloudException;
import com.yyk.config.commonService.response.YYKCloudResponse;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;

/**
 * @author ljp
 * @CLASSNAME GlobalExceptionHandler
 * @date 2021/1/18 16:09
 * @description： 统一异常处理
 */

@ControllerAdvice
public class GlobalExceptionHandler {

    @ResponseBody
    @ExceptionHandler(YYKCloudException.class)
    public YYKCloudResponse globalException(HttpServletResponse response, YYKCloudException ex) {

        return YYKCloudResponse.error(ex.getCode(), ex.getMsg());
    }

    @ResponseBody
    @ExceptionHandler(RuntimeException.class)
    public YYKCloudResponse globalException(HttpServletResponse response, RuntimeException ex) {

        return YYKCloudResponse.error(ex.getMessage());
    }

    @ResponseBody
    @ExceptionHandler(Exception.class)
    public YYKCloudResponse globalException(HttpServletResponse response, Exception ex) {

        return YYKCloudResponse.error(HttpStatusEnum.HTTP_INTERNAL_ERROR.getCode(), HttpStatusEnum.HTTP_INTERNAL_ERROR.getDesc());
    }

}
